import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '/index',
      component: () => import('../views/index.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue')
    },
    {
      path: '/index',
      name: 'index',
      component: () => import('../views/index.vue'),
      children: [
        {
          path: '/typemanger',
          name: 'typemanger',
          component: () => import('../views/typeMange.vue')
        },
        {
          path: '/welcome',
          name: 'welcome',
          component: () => import('../views/welcome.vue')
        },
        {
          path: '/commoditymange',
          name: 'commoditymange',
          component: () => import('../views/commodityManage.vue')
        },
        {
          path: '/ordermange',
          name: 'ordermange',
          component: () => import('../views/orderManage.vue')
        },
        {
          path: '/ordertotal',
          name: 'ordertotal',
          component: () => import('../views/orderTotal.vue')
        },
        {
          path: '/buy',
          name: 'buy',
          component: () => import('../views/buy.vue')
        },
        {
          path: '/car',
          name: 'car',
          component: () => import('../views/car.vue')
        },
        {
          path: '/myorder',
          name: 'myorder',
          component: () => import('../views/myorder.vue')
        },
        {
          path: '/myinfo',
          name: 'myinfo',
          component: () => import('../views/myinfo.vue')
        }
      ]
    },
  ]
})

export default router

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('userRole')
  if (to.path === '/login' || token) {
    next()
  } else {
    next('/login')
  }
})
